package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDTO;
import com.zzyl.nursing.service.NursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
/**
 * 护理计划Service业务层处理
 *
 * @author wjw
 * @date 2025-09-12
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //1. 查询计划
        NursingPlan plan = getById(id);
        if (plan == null){
            throw new RuntimeException("计划不存在");
        }
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        //2. 把plan中的信息装进PlanVo中
        BeanUtils.copyProperties(plan,nursingPlanVo); // 属性的数据类型不一致复制不了
        //2. 查询计划中的项目
        //两种方式: 1. 手动写sql; 2. 使用MyBatisPlus的方法(不可用???)
        List<NursingProjectPlanVo> list = nursingProjectPlanService.listByPlanId(id); // MyBatis返回值与查询值数据类型不一致可以自动转换
        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO dto)
    {
        //0. 复制属性值
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(dto,plan);
        //1. 先插入计划
        save(plan);
        //2. 在插入计划中的项目: 设置id
        if (dto.getProjectPlans()!= null && dto.getProjectPlans().size() > 0){
            //循环设置id
            dto.getProjectPlans().stream().forEach(pro -> pro.setPlanId(plan.getId()));
            //两种方式 : 1. 自己手搓xml循环插入; 2. 调用Service的MyBatisPlus的批量插入方法
            nursingProjectPlanService.saveBatch(dto.getProjectPlans());
        }
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDTO dto)
    {
        //1. 先修改计划
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(dto,plan);
        updateById(plan);
        //2. 判断是否有项目,有项目就删除项目再添加
        if (dto.getProjectPlans()!=null && dto.getProjectPlans().size()>0){
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            projectPlans.stream().forEach(p->p.setPlanId(dto.getId()));
            //先删除
//            nursingProjectPlanService.removeById(dto.getId());
            nursingProjectPlanService.deleteByPlanId(dto.getId());
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        //1. 先删除相关的护理项目
        nursingProjectPlanService.deleteByPlanId(id);
        //2. 再删除护理计划
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlan> getAll() {
        return list();
    }
}
